package com.ruoyi.oss.event.listener;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.constant.CacheNames;
import com.ruoyi.common.utils.cache.CacheUtils;
import com.ruoyi.common.utils.cache.RedisUtils;
import com.ruoyi.oss.constant.OssConstant;
import com.ruoyi.oss.domain.SysOssConfig;
import com.ruoyi.oss.event.domain.UpdateConfigCacheEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class UpdateConfigCacheListener {
    @Autowired
    RedisUtils redisUtils;

    /**
     * 更新配置缓存
     *
     * @param event 配置事件
     */
    @EventListener
    public void updateConfigCache(UpdateConfigCacheEvent event) {
        SysOssConfig config = event.getSysOssConfig();
        if(ObjectUtil.isNull(config)) {
            return;
        }
        CacheUtils.put(CacheNames.SYS_OSS_CONFIG, config.getConfigKey(), config);
        redisUtils.publish(OssConstant.DEFAULT_CONFIG_KEY, config.getConfigKey(), msg -> {
            log.info("发布刷新OSS配置 => " + msg);
        });
    }
}
